package com.forestar.mapControl.mappool;

import android.graphics.Rect;
import android.graphics.RectF;

import com.forestar.mapControl.mapview.multitouch.LayerInfo;
import com.forestar.mapControl.utils.bitmapcache.CacheableBitmapDrawable;

import java.util.LinkedList;


/**
 * 创建时间：2018/8/8
 * 作者: petra
 * 说明：瓦片对象，提供瓦片索引和瓦片简单的计算。
 **/
public class MzTileBean implements MzTileConstants
  {

    //瓦片状态，未下载，下载中，已完成，下载失败
    public static final int LOAD_STATE_NO=1;
    public static final int LOAD_STATE_LOADING=2;
    public static final int LOAD_STATE_COMPLETE=3;
    public static final int LOAD_STATE_FAIL=4;
    public static final int LOAD_STATE_SHOWING=5;

    public final int level;
    public final int x;
    public final int y;
    //目前的加载状态
    private int state = LOAD_STATE_NO;
    private final String name;
    private Rect tileRect;
    private final String cacheKey;
      LinkedList
    //图层id
    private LayerInfo layerID;
    //请求id
    private int displayStatusId;

    //新增屏幕所需的 CacheableBitmapDrawable
    private CacheableBitmapDrawable cacheableBitmapDrawable;

    public MzTileBean(String name, int level, int x, int y, LayerInfo layerID)
      {
        this.name = name;
        this.level = level;
        this.x = x;
        this.y = y;
        this.layerID = layerID;
        cacheKey = name + (new StringBuilder()).append('/').append(level).append('/').append(x).append('/').append(y).toString();
      }
//构建瓦片唯一key的方法
      public static final String buildCacheKey(String name, int level, int x, int y){
        return name + (new StringBuilder()).append('/').append(level).append('/').append(x).append('/').append(y).toString();
      }


    public String getRelativePath()
      {
        //zq--瓦片存储路径格式：level/x[x/128]y[y/128]/x[x%128]y[y%128].mzt
        return String.format("%d/x%dy%d/x%dy%d%s", level, x / REGION_TILE_COUNT, y / REGION_TILE_COUNT, x % REGION_TILE_COUNT, y % REGION_TILE_COUNT, MZTILE_SUFFIX);
      }

    public String getRelativePath(int index)
      {//测试函数，用来加载不同压缩比的影像,0-jpg(100%),1-webp(100%),2-webp(80%),3-webp(60%),4-webp(40%)
        //zq--瓦片存储路径格式：level/x[x/128]y[y/128]/x[x%128]y[y%128].mzt
        return String.format("%d/x%dy%d/x%dy%d%s", level, x / REGION_TILE_COUNT, y / REGION_TILE_COUNT, x % REGION_TILE_COUNT, y % REGION_TILE_COUNT, MZTILE_SUFFIX + index);
      }

    public int getDisplayStatusId() {
      return displayStatusId;
    }

    public void setDisplayStatusId(int displayStatusId) {
      this.displayStatusId = displayStatusId;
    }

    public int getLevel()
      {
        return level;
      }

    public int getX()
      {
        return x;
      }

    public int getY()
      {
        return y;
      }

    public void setTileRect(final Rect rect)
      {
        tileRect = rect;
      }

    public final Rect getTileRect()
      {
        return tileRect;
      }

    public String getName()
      {
        return name;
      }

    public String getCacheKey()
      {
        return cacheKey;
      }

    public void setState(int state)
      {
        this.state = state;
      }

    public int getState()
      {
        return state;
      }

    public CacheableBitmapDrawable getCacheableBitmapDrawable() {
      return cacheableBitmapDrawable;
    }

    public void setCacheableBitmapDrawable(CacheableBitmapDrawable cacheableBitmapDrawable) {
      this.cacheableBitmapDrawable = cacheableBitmapDrawable;
    }

    @Override
    public String toString() {
      return "MzTileBean{" +
              "level=" + level +
              ", x=" + x +
              ", y=" + y +
              ", state=" + state +
              ", name='" + name + '\'' +
              ", tileRect=" + tileRect +
              ", cacheKey='" + cacheKey  +
              '}';
    }

    public LayerInfo getLayerID() {
      return layerID;
    }

    public void setLayerID(LayerInfo layerID) {
      this.layerID = layerID;
    }
  }
